package com.geeklan.smart.ui.banner;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class CustomIndicatorView extends View {
    private int totalCount = 5;  // 默认的页面数量
    private int currentPosition = 0;  // 当前选中的页面
    private int circleSize = 8;  // 圆形指示器的半径
    private int rectangleWidth = 36;  // 矩形指示器的宽度（增加为当前的2倍）
    private int indicatorHeight = 12;  // 矩形指示器的高度（与圆形高度一致）
    private int indicatorSpacing = 10;  // 指示器之间的间距

    private Paint indicatorPaint;
    private Paint selectedIndicatorPaint;

    public CustomIndicatorView(Context context) {
        super(context);
        init();
    }

    public CustomIndicatorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomIndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    // 初始化画笔
    private void init() {
        indicatorPaint = new Paint();
        indicatorPaint.setColor(Color.WHITE);  // 未选中时的颜色
        indicatorPaint.setAntiAlias(true);

        selectedIndicatorPaint = new Paint();
        selectedIndicatorPaint.setColor(Color.WHITE);  // 选中时的颜色
        selectedIndicatorPaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 计算指示器的宽度
        int totalWidth = (rectangleWidth + indicatorSpacing) * totalCount - indicatorSpacing;
        int startX = (getWidth() - totalWidth) / 2;  // 将指示器居中显示

        // 绘制每个指示器
        for (int i = 0; i < totalCount; i++) {
            // 根据当前位置绘制选中或未选中的指示器
            if (i == currentPosition) {
                // 选中的矩形指示器
                drawSelectedIndicator(canvas, startX + i * (rectangleWidth + indicatorSpacing));
            } else {
                // 未选中的圆形指示器
                drawUnselectedIndicator(canvas, startX + i * (rectangleWidth + indicatorSpacing));
            }
        }
    }

    // 绘制选中的指示器（圆角矩形）
    private void drawSelectedIndicator(Canvas canvas, float x) {
        float radius = indicatorHeight / 2; // 使用 indicatorHeight 来保证矩形的高度与圆形相等

        // 计算矩形的位置，使其居中于圆形
        float rectLeft = x + (circleSize - rectangleWidth / 2);  // 计算矩形的左边距，确保矩形居中
        float rectRight = rectLeft + rectangleWidth;
        RectF rect = new RectF(rectLeft, getHeight() / 2 - radius, rectRight, getHeight() / 2 + radius);
        canvas.drawRoundRect(rect, radius, radius, selectedIndicatorPaint);
    }

    // 绘制未选中的指示器（圆形）
    private void drawUnselectedIndicator(Canvas canvas, float x) {
        // 绘制较小的圆形指示器
        canvas.drawCircle(x + circleSize, getHeight() / 2, circleSize, indicatorPaint);
    }

    // 设置页面数量
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
        invalidate();
    }

    // 设置当前选中的页面
    public void setCurrentPosition(int currentPosition) {
        this.currentPosition = currentPosition;
        invalidate();
    }

    // 设置圆形指示器的大小
    public void setCircleSize(int circleSize) {
        this.circleSize = circleSize;
        invalidate();
    }

    // 设置矩形指示器的宽度
    public void setRectangleWidth(int rectangleWidth) {
        this.rectangleWidth = rectangleWidth;
        invalidate();
    }

    // 设置指示器之间的间距
    public void setIndicatorSpacing(int indicatorSpacing) {
        this.indicatorSpacing = indicatorSpacing;
        invalidate();
    }
}
